<?php
include_once("error.php");
class affichage{
	private $txt;
	private $tab;
	public function __construct(){
		$this->txt="";
		$this->tab=array();
	}
	public function setStyle($value){
		$this->style=$value;
	}
	public function setTitle($value){
		$this->titre=$value;
	}
	public function append($txt){
		$this->txt.=$txt;
	}
	public function add($nom,$txt){
		$this->tab[$nom]=urlencode($txt);
	}
	public function println($txt){
		$this->append($txt."<br/>");
	}
	public function show(){
		$error=Error::getInstance();
		if($error->length()>0){
			echo $error->getErrors();
		}else{
			$result="";
			foreach ($this->tab as $k => $v) {
			    $result.=$k."=".$v."&";
			}
			if(strlen($this->txt)>0){
				$result.=$this->txt;
			}else{
				$result=substr($result,0,strlen($result)-1);
			}
			if(strlen($result)>0){
				return $result;
			}
		}
	}
	public function error($txt){
		$error=Error::getInstance();
		$error->append($txt);
	}
}
class XMLR{
	private $identifiant;
	private $xmlr;
	public function __construct($identifiant="XMLR"){
		$this->identifiant=$identifiant;
		$this->xmlr=array();
	}
	public function addFunction($objet,$fonction){
		$nb=func_num_args();
		$tab=array();
		if($nb>2){
			for($i=2;$i<$nb;$i++){
				$tab[$i-2]=func_get_arg($i);
			}
		}
		if(!isset($this->xmlr[$objet])){
			$this->xmlr[$objet]=array();
		}
		if(!isset($this->xmlr[$objet][$fonction])){
			$this->xmlr[$objet][$fonction]=array();
		}
		$this->xmlr[$objet][$fonction][]=$tab;
	}
	public function toString(){
		$x="<".$this->identifiant.">";
		$x.=$this->tab2XML($this->xmlr);
		$x.="</".$this->identifiant.">";
		return $x;
	}
	private function tab2XML($tab,$cle=""){
		if(!is_array($tab)){
			return $tab;
		}else{
			$result="";
			foreach($tab as $key=>$value){
				if(!is_numeric($key)){
					$result.="<".$key.">";
					$result.=$this->tab2XML($value,$key);
					$result.="</".$key.">";
				}else{
					$showcle=$this->getType($value);
					if($showcle=="array" && !is_numeric($cle)){
						$showcle=$cle;
						if($key==0 || $key==sizeOf($tab)-1){
							$showcle="";
						}
					}
					if($showcle!=""){
						$result.="<".$showcle.">";
					}else{
						if($key>0){
							$result.="<".$cle.">";
						}
					}
					
				//	$result.=($showcle!="boolean")?$this->tab2XML($value):($this->tab2XML($value)=="1"?"true":"false");//convertit en boolean pas nécessaire mais plus logique
					if($showcle!="boolean"){
						$result.=$this->tab2XML($value);
					}else{
						if($value){
							$result.="true";
						}else{
							$result.="false";
						}
					}
					if($showcle!=""){
						$result.="</".$showcle.">";
					}else{
						if($key<sizeOf($tab)-1){
							$result.="</".$cle.">";
						}
					}
				}
			}
			return $result;		
		}
	}
	private function getType($valeur){
		if(!isset($valeur)){
			return "null";
		}
		if(is_numeric($valeur)){
			if(floor($valeur)==$valeur){
				if($valeur<0){
					return "int";
				}else{
					return "uint";
				}
			}else{
				return "number";
			}
		}
		if(is_bool($valeur)){
			return "boolean";
		}
		if(is_array($valeur)){
			return "array";
		}
		return "string";
	}
}

?>
